View Javadoc

1   package com.nexes.wizard;
2   
3   import java.beans.*;
4   import java.util.HashMap;
5   
6   import javax.swing.Icon;
7   
8   /***
9    * The model for the Wizard component, which tracks the text, icons, and enabled state
10   * of each of the buttons, as well as the current panel that is displayed. Note that 
11   * the model, in its current form, is not intended to be subclassed. 
12   */
13  
14  
15  public class WizardModel {
16  
17      /***
18       * Identification string for the current panel.
19       */    
20      public static final String CURRENT_PANEL_DESCRIPTOR_PROPERTY = "currentPanelDescriptorProperty";
21      
22      /***
23       * Property identification String for the Back button's text
24       */    
25      public static final String BACK_BUTTON_TEXT_PROPERTY = "backButtonTextProperty";
26      /***
27       * Property identification String for the Back button's icon
28       */    
29      public static final String BACK_BUTTON_ICON_PROPERTY = "backButtonIconProperty";
30      /***
31       * Property identification String for the Back button's enabled state
32       */    
33      public static final String BACK_BUTTON_ENABLED_PROPERTY = "backButtonEnabledProperty";
34  
35      /***
36       * Property identification String for the Next button's text
37       */    
38      public static final String NEXT_FINISH_BUTTON_TEXT_PROPERTY = "nextButtonTextProperty";
39      /***
40       * Property identification String for the Next button's icon
41       */    
42      public static final String NEXT_FINISH_BUTTON_ICON_PROPERTY = "nextButtonIconProperty";
43      /***
44       * Property identification String for the Next button's enabled state
45       */    
46      public static final String NEXT_FINISH_BUTTON_ENABLED_PROPERTY = "nextButtonEnabledProperty";
47      
48      /***
49       * Property identification String for the Cancel button's text
50       */    
51      public static final String CANCEL_BUTTON_TEXT_PROPERTY = "cancelButtonTextProperty";
52      /***
53       * Property identification String for the Cancel button's icon
54       */    
55      public static final String CANCEL_BUTTON_ICON_PROPERTY = "cancelButtonIconProperty";
56      /***
57       * Property identification String for the Cancel button's enabled state
58       */    
59      public static final String CANCEL_BUTTON_ENABLED_PROPERTY = "cancelButtonEnabledProperty";
60      
61      private WizardPanelDescriptor currentPanel;
62      
63      private HashMap panelHashmap;
64      
65      private HashMap buttonTextHashmap;
66      private HashMap buttonIconHashmap;
67      private HashMap buttonEnabledHashmap;
68      
69      private PropertyChangeSupport propertyChangeSupport;
70      
71      
72      /***
73       * Default constructor.
74       */    
75      public WizardModel() {
76          
77          panelHashmap = new HashMap();
78          
79          buttonTextHashmap = new HashMap();
80          buttonIconHashmap = new HashMap();
81          buttonEnabledHashmap = new HashMap();
82          
83          propertyChangeSupport = new PropertyChangeSupport(this);
84  
85      }
86      
87      /***
88       * Returns the currently displayed WizardPanelDescriptor.
89       * @return The currently displayed WizardPanelDescriptor
90       */    
91      WizardPanelDescriptor getCurrentPanelDescriptor() {
92          return currentPanel;
93      }
94      
95      /***
96       * Registers the WizardPanelDescriptor in the model using the Object-identifier specified.
97       * @param id Object-based identifier
98       * @param descriptor WizardPanelDescriptor that describes the panel
99       */    
100      void registerPanel(Object id, WizardPanelDescriptor descriptor) {
101         
102         //  Place a reference to it in a hashtable so we can access it later
103         //  when it is about to be displayed.
104         
105         panelHashmap.put(id, descriptor);
106         
107     }  
108     
109     /***
110      * Sets the current panel to that identified by the Object passed in.
111      * @param id Object-based panel identifier
112      * @return boolean indicating success or failure
113      */    
114      boolean setCurrentPanel(Object id) {
115 
116         //  First, get the hashtable reference to the panel that should
117         //  be displayed.
118         
119         WizardPanelDescriptor nextPanel =
120             (WizardPanelDescriptor)panelHashmap.get(id);
121         
122         //  If we couldn't find the panel that should be displayed, return
123         //  false.
124         
125         if (nextPanel == null)
126             throw new WizardPanelNotFoundException();   
127 
128         WizardPanelDescriptor oldPanel = currentPanel;
129         currentPanel = nextPanel;
130         
131         if (oldPanel != currentPanel)
132             firePropertyChange(CURRENT_PANEL_DESCRIPTOR_PROPERTY, oldPanel, currentPanel);
133         
134         return true;
135         
136     }
137 
138     Object getBackButtonText() {
139         return buttonTextHashmap.get(BACK_BUTTON_TEXT_PROPERTY);
140     }
141     
142     void setBackButtonText(Object newText) {
143         
144         Object oldText = getBackButtonText();        
145         if (!newText.equals(oldText)) {
146             buttonTextHashmap.put(BACK_BUTTON_TEXT_PROPERTY, newText);
147             firePropertyChange(BACK_BUTTON_TEXT_PROPERTY, oldText, newText);
148         }
149     }
150 
151     Object getNextFinishButtonText() {
152         return buttonTextHashmap.get(NEXT_FINISH_BUTTON_TEXT_PROPERTY);
153     }
154     
155     void setNextFinishButtonText(Object newText) {
156         
157         Object oldText = getNextFinishButtonText();        
158         if (!newText.equals(oldText)) {
159             buttonTextHashmap.put(NEXT_FINISH_BUTTON_TEXT_PROPERTY, newText);
160             firePropertyChange(NEXT_FINISH_BUTTON_TEXT_PROPERTY, oldText, newText);
161         }
162     }
163 
164     Object getCancelButtonText() {
165         return buttonTextHashmap.get(CANCEL_BUTTON_TEXT_PROPERTY);
166     }
167     
168     void setCancelButtonText(Object newText) {
169         
170         Object oldText = getCancelButtonText();        
171         if (!newText.equals(oldText)) {
172             buttonTextHashmap.put(CANCEL_BUTTON_TEXT_PROPERTY, newText);
173             firePropertyChange(CANCEL_BUTTON_TEXT_PROPERTY, oldText, newText);
174         }
175     } 
176     
177     Icon getBackButtonIcon() {
178         return (Icon)buttonIconHashmap.get(BACK_BUTTON_ICON_PROPERTY);
179     }
180     
181     void setBackButtonIcon(Icon newIcon) {
182         
183         Object oldIcon = getBackButtonIcon();        
184         if (!newIcon.equals(oldIcon)) {
185             buttonIconHashmap.put(BACK_BUTTON_ICON_PROPERTY, newIcon);
186             firePropertyChange(BACK_BUTTON_ICON_PROPERTY, oldIcon, newIcon);
187         }
188     }
189 
190     Icon getNextFinishButtonIcon() {
191         return (Icon)buttonIconHashmap.get(NEXT_FINISH_BUTTON_ICON_PROPERTY);
192     }
193     
194     public void setNextFinishButtonIcon(Icon newIcon) {
195         
196         Object oldIcon = getNextFinishButtonIcon();        
197         if (!newIcon.equals(oldIcon)) {
198             buttonIconHashmap.put(NEXT_FINISH_BUTTON_ICON_PROPERTY, newIcon);
199             firePropertyChange(NEXT_FINISH_BUTTON_ICON_PROPERTY, oldIcon, newIcon);
200         }
201     }
202 
203     Icon getCancelButtonIcon() {
204         return (Icon)buttonIconHashmap.get(CANCEL_BUTTON_ICON_PROPERTY);
205     }
206     
207     void setCancelButtonIcon(Icon newIcon) {
208         
209         Icon oldIcon = getCancelButtonIcon();        
210         if (!newIcon.equals(oldIcon)) {
211             buttonIconHashmap.put(CANCEL_BUTTON_ICON_PROPERTY, newIcon);
212             firePropertyChange(CANCEL_BUTTON_ICON_PROPERTY, oldIcon, newIcon);
213         }
214     } 
215         
216     
217     Boolean getBackButtonEnabled() {
218         return (Boolean)buttonEnabledHashmap.get(BACK_BUTTON_ENABLED_PROPERTY);
219     }
220     
221     void setBackButtonEnabled(Boolean newValue) {
222         
223         Boolean oldValue = getBackButtonEnabled();        
224         if (newValue != oldValue) {
225             buttonEnabledHashmap.put(BACK_BUTTON_ENABLED_PROPERTY, newValue);
226             firePropertyChange(BACK_BUTTON_ENABLED_PROPERTY, oldValue, newValue);
227         }
228     }
229 
230     Boolean getNextFinishButtonEnabled() {
231         return (Boolean)buttonEnabledHashmap.get(NEXT_FINISH_BUTTON_ENABLED_PROPERTY);
232     }
233     
234     void setNextFinishButtonEnabled(Boolean newValue) {
235         
236         Boolean oldValue = getNextFinishButtonEnabled();        
237         if (newValue != oldValue) {
238             buttonEnabledHashmap.put(NEXT_FINISH_BUTTON_ENABLED_PROPERTY, newValue);
239             firePropertyChange(NEXT_FINISH_BUTTON_ENABLED_PROPERTY, oldValue, newValue);
240         }
241     }
242     
243     Boolean getCancelButtonEnabled() {
244         return (Boolean)buttonEnabledHashmap.get(CANCEL_BUTTON_ENABLED_PROPERTY);
245     }
246     
247     void setCancelButtonEnabled(Boolean newValue) {
248         
249         Boolean oldValue = getCancelButtonEnabled();        
250         if (newValue != oldValue) {
251             buttonEnabledHashmap.put(CANCEL_BUTTON_ENABLED_PROPERTY, newValue);
252             firePropertyChange(CANCEL_BUTTON_ENABLED_PROPERTY, oldValue, newValue);
253         }
254     }
255     
256     
257     
258     public void addPropertyChangeListener(PropertyChangeListener p) {
259         propertyChangeSupport.addPropertyChangeListener(p);
260     }
261     
262     public void removePropertyChangeListener(PropertyChangeListener p) {
263         propertyChangeSupport.removePropertyChangeListener(p);
264     }
265     
266     protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
267         propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
268     }
269     
270 }